/*express web框架的应用文件
1.导入所需模块
2.导入路由模块
3.应用中间件
4.定义路由
5.到处express应用实例 */


//1.导入所需模块

//导入express模块
var express = require('express');
//导入node.js的path模块
var path = require('path');
//导入cookie-parser模块
var cookieParser = require('cookie-parser');
//导入日志模块
var logger = require('morgan');
//导入swagger-ui-express模块
const swaggerUi = require('swagger-ui-express');
//导入swagger-jsdoc模块
const swaggerJSDoc = require('swagger-jsdoc');

//2.导入路由模块

//默认路由
var indexRouter = require('./routes/index');
//用户路由
var usersRouter = require('./routes/users');


//生成应用实例app
var app = express();


//3.应用中间层

//开发日志
app.use(logger('dev'));
//json解析
app.use(express.json());
//url解析
app.use(express.urlencoded({ extended: false }));
//cookie解析
app.use(cookieParser());
//静态资源解析
app.use(express.static(path.join(__dirname, 'public')));
//中间件:swagger
const options = {
    definition: {
        openapi: '3.0.0',
        info: {
            title: '我的藏书房接口',
            version: '1.0.0',
            },
        },
        apis:[path.join(__dirname, "./routes/*.js")]
    };

    const openapiSpecification = swaggerJSDoc(options);
    app.use('/dev-api', swaggerUi.serve, swaggerUi.setup(openapiSpecification));


//4.定义路由

//默认路由
app.use('/', indexRouter);
//用户路由
app.use('/users', usersRouter);


//5.导出express应用实例
module.exports = app;
/*ceshi */